﻿using Gma.System.MouseKeyHook;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ColorTool
{
    public partial class FrmMain : Form
    {
        private IKeyboardMouseEvents _globalHook;
        private bool _isSelected = false;
        public FrmMain()
        {
            InitializeComponent();
            _globalHook = Hook.GlobalEvents();
            _globalHook.MouseMoveExt += _globalHook_MouseMoveExt;
            _globalHook.MouseClick += _globalHook_MouseClick;
        }

        private void _globalHook_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.colorPicker.Checked)
            {
                if (e.Button == MouseButtons.Left)
                {
                    _isSelected = true;
                    this.promptLab.Visible = true;
                }
            }
            
        }

        /// <summary>
        /// 全局鼠标移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _globalHook_MouseMoveExt(object sender, MouseEventExtArgs e)
        {
            if (this.colorPicker.Checked)
            {
                if (!_isSelected)
                {
                    selectedColor(GetScreenColorUsingPInvoke(e.X, e.Y));
                }
            }
        }


        private void colorPanel_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if(this.colorSelectRbtn.Checked)
            {
                DialogResult result = this.colorDialog.ShowDialog();
                if (result == DialogResult.OK)
                {
                    selectedColor(this.colorDialog.Color);
                }
            }
            if (this.colorPicker.Checked && this.promptLab.Visible)
            {
                _isSelected = false;
                this.promptLab.Visible = false;
            }
        }
        /// <summary>
        /// 获取选定的颜色的各项色值
        /// </summary>
        /// <param name="color">选定的颜色</param>
        private void selectedColor(Color color)
        {
            this.colorPanel.BackColor = color;
            this.htmlTxtBox.Text = ColorTranslator.ToHtml(color).ToUpper();
            this.htmlTxtBox.ForeColor = color;
            this.oleTxtBox.Text = ColorTranslator.ToOle(color).ToString();
            this.oleTxtBox.ForeColor = color;
            this.win32TxtBox.Text = ColorTranslator.ToWin32(color).ToString();
            this.win32TxtBox.ForeColor = color;
            this.aTxtBox.Text = color.A.ToString();
            this.aTxtBox.ForeColor = color;
            this.rTxtBox.Text = color.R.ToString();
            this.rTxtBox.ForeColor = color;
            this.gTxtBox.Text = color.G.ToString();
            this.gTxtBox.ForeColor = color;
            this.bTxtBox.Text = color.B.ToString();
            this.bTxtBox.ForeColor = color;
        }

        [DllImport("user32.dll")]
        public static extern IntPtr SetSystemCursor(IntPtr hcur, int type);

        private void colorPicker_CheckedChanged(object sender, System.EventArgs e)
        {
            if (this.colorPicker.Checked)
            {
                this.Cursor = Cursors.Cross;
                //SetSystemCursor(Cursors.Cross.Handle, 0);
            }
            else
            {
                this.Cursor = Cursors.Default;
                //SetSystemCursor(Cursors.Default.Handle, 0);
            }
        }

        [DllImport("user32.dll")]
        public static extern IntPtr GetDC(IntPtr hWnd);
        [DllImport("gdi32.dll")]
        public static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
        [DllImport("user32.dll")]
        public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

        public Color GetScreenColorUsingPInvoke(int x, int y)
        {
            IntPtr hdc = GetDC(IntPtr.Zero); // 获取整个屏幕的DC
            uint color = GetPixel(hdc, x, y); // 获取指定位置的颜色
            ReleaseDC(IntPtr.Zero, hdc); // 释放DC资源
            return Color.FromArgb((int)(color & 0x000000FF), (int)((color & 0x0000FF00) >> 8), (int)((color & 0x00FF0000) >> 16)); // 注意：这里的颜色顺序可能需要调整以匹配你的系统设置（通常是BGR）
        }
    }
}
